package com.cmict.data.form.api.util;

import cn.hutool.core.util.ObjectUtil;
import com.cmict.data.form.api.entity.vo.SysFieldFormListVo;
import com.cmict.data.form.api.entity.vo.SysFieldRuleVo;
import com.cmict.data.form.api.entity.vo.SysFormFieldRulesVo;

import java.util.*;

/**
 * @description: 表单规则工具类
 * @author: qiaoyu
 * @create: 2022-06-21 22:57
 **/
public class RuleUtil {


	public static Boolean newRule(SysFieldRuleVo rule, List<SysFieldFormListVo> fieldFormList) {
		Boolean ret = false;
		for(SysFieldFormListVo form : fieldFormList){
			if(rule.getRuleFiledName().equals(form.getFiledName())){
				ret = CountUtil.count(rule.getRuleValue(),rule.getSumFiled(),form.getValue());
			}
		}
		return ret;
	}

	public static Boolean sumRule(Boolean rules, String relat,Boolean first) {
		if(ObjectUtil.isNotEmpty(relat)){
			if (relat.equals("or")) {
				if (rules || first) {
					return true;
				}
				if (!first && !rules) {
					return false;
				}
			} else {
				if (!rules || !first) {
					return false;
				}
			}
			return true;
		}
		return rules;
	}

	/*public static Boolean rule(SysFormFieldRulesVo sysFormFieldRulesVo) {
		Boolean rule = false;
		if(ObjectUtil.isNotEmpty(sysFormFieldRulesVo.getData())){
			for(SysFieldRulesListVo vo:sysFormFieldRulesVo.getData()){
				for(SysFieldFormListVo form:sysFormFieldRulesVo.getFormList()){
					if(vo.getRuleFiledName().equals(form.getFiledName())){
						rule = CountUtil.count(vo.getRuleValue(),vo.getSumFiled(),form.getValue());
					}
				}
			}
		}
		return rule;
	}*/

	/*public static Boolean ruleGroup(SysFormFieldRulesVo sysFormFieldRulesVo) {
		Boolean ruleGroup = false;
		if(ObjectUtil.isNotEmpty(sysFormFieldRulesVo.getFieldRulesListTeam())){
			for(SysFieldRulesListTeamVo vo: sysFormFieldRulesVo.getFieldRulesListTeam()){
				Boolean ruleOne = false;
				Boolean ruleTwo = false;
				for(SysFieldFormListVo form:sysFormFieldRulesVo.getFormList()){
					if(vo.getSysFieldRulesListVo().get(0).getRuleFiledName().equals(form.getFiledName())){
						ruleOne = CountUtil.count(vo.getSysFieldRulesListVo().get(0).getRuleValue(),vo.getSysFieldRulesListVo().get(0).getSumFiled(),form.getValue());
					}
					if(vo.getSysFieldRulesListVo().get(1).getRuleFiledName().equals(form.getFiledName())){
						ruleTwo = CountUtil.count(vo.getSysFieldRulesListVo().get(1).getRuleValue(),vo.getSysFieldRulesListVo().get(1).getSumFiled(),form.getValue());
					}
				}
				if(vo.getRelat().equals("or")){
					if(ruleOne || ruleTwo){
						ruleGroup = true;
					}
				}
				if(vo.getRelat().equals("and")){
					if(ruleOne && ruleTwo){
						ruleGroup = true;
					}
				}
			}
		}
		return ruleGroup;
	}*/

	public static Map<String,String> fieldJoin(SysFormFieldRulesVo sysFormFieldRulesVo, Boolean rule, Boolean ruleGroup, String dispaly, String hide) {
		Map<String,String> map = new HashMap<>();
		if(rule && ruleGroup){
			List<String> displayList = Arrays.asList(sysFormFieldRulesVo.getDispalyField().split(","));
			for (int i = 0; i <displayList.size() ; i++) {
				if(!hide.contains(displayList.get(i)) && !dispaly.contains(displayList.get(i))){
					dispaly = dispaly + displayList.get(i) + ", ";
				}
			}
			List<String> hideList = Arrays.asList(sysFormFieldRulesVo.getHiddenField().split(","));
			for (int i = 0; i <hideList.size() ; i++) {
				if(!dispaly.contains(hideList.get(i)) && !hide.contains(hideList.get(i))){
					hide = hide + hideList.get(i) + ", ";
				}
			}
			map.put("dispaly",dispaly);
			map.put("hide",hide);
			return map;
		}
		map.put("dispaly",dispaly);
		map.put("hide",hide);
		return  map;
	}
}

